package com.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.security.sasl.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * Jwt加密和解密
 */
public class JWTTokenUtil {

    private String secret="JO6HN3NGIU25G2FIG8V7VD6CK9B6T2Z5";//密钥

    private long expire= 120*60*4L* 1000; //8小时 //1200秒=20分钟  120*60

    /**
     * 生成jwt token
     */
    public String getEncodeToken(String json) {
        Date nowDate = new Date();
        Date expireDate = new Date(nowDate.getTime() + expire );

        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;    //把一段文件信息进行加密，产生加密字符串，并且设置过期时间

        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setSubject(json + "")//被加密的字符串 （登录用户名称）
                .setIssuedAt(nowDate)//当前系统 时间
                .setExpiration(expireDate)   //过期时间
                .signWith(signatureAlgorithm, secret.getBytes())  // new String(org.apache.commons.codec.binary.Base64.encodeBase64(secret.getBytes()) 把密钥用Base64加密
                .compact();
        //.signWith(SignatureAlgorithm.HS512, secret)
        //.compact();
    }


    private Claims getClaimByToken(String token) { //把generateToken产生的字符串进行解密,得到Claims对象

        try {
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
            return Jwts.parser()
                    .setSigningKey(secret.getBytes())
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            System.out.println("jwt 解密失败");
            e.printStackTrace();;
            return null;
        }
    }


    /**
     * 根据 getEncodeToken生成的toke值进行解码
     * @param request
     * @return
     * @throws AuthenticationException
     */
    public  String getDecodeToken(HttpServletRequest request) throws AuthenticationException {

        String access_token =  request.getParameter("access_token");

        if (access_token==null || access_token.equals("")){
            access_token =   request.getHeader("access_token");
        }
        ;
        Claims claims = getClaimByToken(access_token);//jwtToken.getClaimByToken(access_token);

        if (claims == null || isTokenExpired(claims.getExpiration()) ) {
            throw new AuthenticationException("access_token 过期");
        }

        String tokenVal  = claims.getSubject();//得到登录用户信息

        return tokenVal;
    }

    /**
     * token是否过期
     * @return  true：过期
     */
    public static boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }

    // Getter && Setter
}